package service

import (
	"context"
	"mini-app/config/mysql"
	"mini-app/internal/app/grpc/client"
	"mini-app/internal/app/grpc/protoc/system_admin"
	"mini-app/internal/dao"
	"mini-app/internal/models"
	"time"

	"192.168.1.75/go-pkg/errorx"
)

type Banner struct{}

func (Banner) Create(
	ctx context.Context,
	record models.Banner,
) (string, error) {
	SystemAdminClient, clientErr := client.GetSystemAdminClient()
	if clientErr != nil {
		return "", errorx.New("图片上传失败", -1)
	}
	if record.Name == "" {
		return "", errorx.New("banner名称不能为空", -1)
	}
	if record.Position == 0 {
		return "", errorx.New("banner位置不能为空", -1)
	}
	if record.EndTime < time.Now().Unix() {
		return "", errorx.New("已过期的生效周期", -1)
	}
	if record.Image == "" {
		return "", errorx.New("banner图片不能为空", -1)
	}
	// 确认图片上传
	{
		var req system_admin.ConfirmFileReq
		req.FileUrl = record.Image
		rsp, err1 := SystemAdminClient.ConfirmFile(context.Background(), &req)
		if err1 != nil && rsp.Code != 0 {
			return "", errorx.New("商品图片上传失败", -1)
		}
		record.Image = rsp.FileUrl
	}
	if *record.LinkType > 0 && *record.LinkType != 1 && *record.LinkType != 2 {
		return "", errorx.New("无效的跳转类型", -1)
	}
	if *record.LinkType == 1 {
		if record.OnlineEventID == "" {
			return "", errorx.New("活动id不能为空", -1)
		}
		detail := dao.OnlineEvent{}.Detail(ctx, mysql.NewDB(), record.OnlineEventID)
		if detail == nil {
			return "", errorx.New("无效的活动", -1)
		}
	} else if *record.LinkType == 2 {
		if record.Link == "" {
			return "", errorx.New("链接地址不能为空", -1)
		}
	}
	return dao.Banner{}.Create(ctx, mysql.NewDB(), record)
}
func (Banner) Update(
	ctx context.Context,
	ID string,
	record models.Banner,
) error {
	// 确认banner是否存在
	detail := dao.Banner{}.Detail(ctx, mysql.NewDB(), ID)
	if detail == nil {
		return errorx.New("无效的banner", -1)
	}
	SystemAdminClient, clientErr := client.GetSystemAdminClient()
	if clientErr != nil {
		return errorx.New("图片上传失败", -1)
	}
	// 确认图片
	if record.Image != "" && detail.Image != record.Image {
		var req system_admin.ConfirmFileReq
		req.FileUrl = record.Image
		rsp, err1 := SystemAdminClient.ConfirmFile(context.Background(), &req)
		if err1 != nil && rsp.Code != 0 {
			return errorx.New("商品图片上传失败", -1)
		}
		// 删除旧的图片
		if detail.Image != "" {
			SystemAdminClient.DeleteFile(context.Background(), &system_admin.DeleteFileReq{
				FileUrl: detail.Image,
			})
		}
		record.Image = rsp.FileUrl
	}
	// 参数确认
	if *record.LinkType > 0 && *record.LinkType != 1 && *record.LinkType != 2 {
		return errorx.New("无效的跳转类型", -1)
	}
	if *record.LinkType == 1 {
		if record.OnlineEventID != "" {
			detail := dao.OnlineEvent{}.Detail(ctx, mysql.NewDB(), record.OnlineEventID)
			if detail == nil {
				return errorx.New("无效的活动", -1)
			}
		}
	}
	return dao.Banner{}.Update(ctx, mysql.NewDB(), ID, record)
}
func (Banner) Delete(
	ctx context.Context,
	id string,
) error {
	return dao.Banner{}.Delete(ctx, mysql.NewDB(), id)
}
func (Banner) Detail(
	ctx context.Context,
	id string,
) *models.Banner {
	return dao.Banner{}.Detail(ctx, mysql.NewDB(), id)
}
func (Banner) List(
	ctx context.Context,
	name string,
	position int,
	startTime, endTime int64,
	status int,
	page, limit int,
) (list []models.Banner, total int64) {
	list, total = dao.Banner{}.List(
		ctx,
		mysql.NewDB(),
		name,
		position,
		startTime, endTime,
		status,
		page, limit,
	)
	nowTS := time.Now().Unix()
	for index, record := range list {
		if *record.LinkType == 0 {
			list[index].Link = ""
			list[index].OnlineEventID = ""
		}
		// 活动类型，清空链接地址
		if *record.LinkType == 1 {
			list[index].Link = ""
		}
		if record.Status == 0 {
			if nowTS >= record.StartTime && nowTS <= record.EndTime {
				list[index].Status = 1 // 进行中
			} else if nowTS < record.StartTime {
				list[index].Status = 2 // 未开始
			} else if nowTS > record.EndTime {
				list[index].Status = 3 // 已结束
			}
		}
	}
	return
}
func (Banner) Online(
	ctx context.Context,
	id string,
) error {
	return dao.Banner{}.Online(ctx, mysql.NewDB(), id)
}
func (Banner) Offline(
	ctx context.Context,
	id string,
) error {
	return dao.Banner{}.Offline(ctx, mysql.NewDB(), id)
}
